package com.baseweb.webadmin.alipay.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.baseweb.webadmin.core.model.pojo.ByUserBatchPayOrder;
import com.baseweb.webadmin.core.model.pojo.ByUserOrder;
import com.baseweb.webadmin.core.util.DateUtil;
import com.baseweb.webadmin.core.util.ObjectFormatter;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 支付宝请求参数封装
 *
 */
public class AlipayParams {

	private static final Logger  LOG=LoggerFactory.getLogger(AlipayParams.class);
	
	
	public static   Map<String,String>    getAlipayParamsForMap(HttpServletRequest request){
		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
			params.put(name, valueStr);
			
			LOG.info("支付宝异步通知参数：  "+name+"="+valueStr);
	
		}
		return   params;
	}
	
//	public static  AccountChange   getAlipayParamsForAccountChange(Map<String,String> params){
//	
//	AccountChange account=new AccountChange();
//	account.setAlipayNotifyTime(params.get("notify_time"));
//	account.setAlipayNotifyType(params.get("notify_type"));
//	account.setAlipayNotifyId(params.get("notify_id"));
//	account.setAlipayTradeNo(params.get("trade_no"));
//	account.setAlipayTradeStatus(params.get("trade_status"));
//	account.setAlipaySellerEmail(params.get("seller_email"));
//	account.setAlipaySellerId(params.get("seller_id"));
//	account.setAlipayTotalFee(params.get("total_fee"));
//	account.setAlipayGmtCreate(params.get("gmt_create"));
//	account.setAlipayGmtPayment(params.get("gmt_payment"));
//	
//	account.setOrderNum(params.get("out_trade_no"));
//	account.setRechargeTime(params.get("gmt_payment"));
//	account.setRechargeMode(Const.AccountRechargeMode.ALIPAY);
//	Integer changeValue=(int) (Double.parseDouble(params.get("total_fee"))*Env.getEasyMoneyRechargeGiveRatio());
//	account.setChangeValue(changeValue);
//	account.setRechargeMoney(Double.parseDouble(params.get("total_fee")));
//	return account;
//}
	
	
	public static  ByUserOrder   getAlipayParamsForOrder(ByUserOrder order, Map<String,String> params){

//		ByUserOrder order=new ByUserOrder();
		try{
			order.setAlipaynotifytime(DateUtil.getDateTimeDetail(params.get("notify_time")));
			order.setAlipaynotifytype(params.get("notify_type"));
			order.setAlipaynotifyid(params.get("notify_id"));
			order.setAlipaytradeno(params.get("trade_no"));
			order.setAlipaytradestatus(params.get("trade_status"));
			order.setAlipayselleremail(params.get("seller_email"));
			order.setAlipaysellerid(params.get("seller_id"));
			String totalAmount = params.get("total_amount") == null ? params.get("total_fee") : params.get("total_amount");
			totalAmount = totalAmount == null ? "0" : totalAmount;
			order.setAlipaytotalfee(ObjectFormatter.str2BigDecimal(totalAmount));

		}catch (Exception e){
			e.printStackTrace();
		}
//		order.setAlipaygmtcreate(DateUtil.getDateTimeDetail(params.get("gmt_create")));
//		order.setAlipaygmtpayment(DateUtil.getDateTimeDetail(params.get("gmt_payment")));
		
//		order.setId(Integer.valueOf(params.get("out_trade_no")));
//		order.setActualTotalPay(Double.parseDouble(params.get("total_fee")));
		
		return order;
	}

	public static ByUserBatchPayOrder getAlipayParamsForBatchPayOrder(Map<String,String> params){

		ByUserBatchPayOrder order=new ByUserBatchPayOrder();

		order.setAlipaynotifytime(DateUtil.getDateTimeDetail(params.get("notify_time")));
		order.setAlipaynotifytype(params.get("notify_type"));
		order.setAlipaynotifyid(params.get("notify_id"));
		order.setAlipaytradeno(params.get("trade_no"));
		order.setAlipaytradestatus(params.get("trade_status"));
		order.setAlipayselleremail(params.get("seller_email"));
		order.setAlipaysellerid(params.get("seller_id"));
		order.setAlipaytotalfee(ObjectFormatter.str2BigDecimal(params.get("total_fee")));
		order.setAlipaygmtcreate(DateUtil.getDateTimeDetail(params.get("gmt_create")));
		order.setAlipaygmtpayment(DateUtil.getDateTimeDetail(params.get("gmt_payment")));

		order.setBatchNum(params.get("out_trade_no"));
//		order.setActualTotalPay(Double.parseDouble(params.get("total_fee")));

		return order;
	}
	
	
//	public static  OrdersBusiness   getAlipayParamsForOrdersBusiness(Map<String,String> params){
//		OrdersBusiness order=new OrdersBusiness();
//
//		order.setBatchNo(params.get("batch_no"));
//
//		order.setAlipayNotifyId(params.get("notify_id"));
//		order.setAlipayNotifyTime(params.get("notify_time"));
//
//		order.setAlipaySuccessNum(params.get("success_num"));
//		order.setAlipayResultDetails(params.get("result_details"));
//
//
//		return order;
//	}

	
	
	
}
